package com.southminority.ethnic.service.example;

import com.southminority.ethnic.common.context.UserContext;
import com.southminority.ethnic.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 示例Service
 * 演示如何在其他Service中使用UserContext获取当前登录用户信息
 * 
 * @author SouthMinority
 */
@Slf4j
@Service
public class ExampleService {

    /**
     * 示例方法：获取当前登录用户信息
     */
    public User getCurrentUserInfo() {
        User currentUser = UserContext.getCurrentUser();
        if (currentUser == null) {
            throw new RuntimeException("用户未登录");
        }
        
        log.info("当前登录用户: {}", currentUser.getEmail());
        return currentUser;
    }

    /**
     * 示例方法：获取当前用户ID
     */
    public Long getCurrentUserId() {
        Long userId = UserContext.getCurrentUserId();
        if (userId == null) {
            throw new RuntimeException("用户未登录");
        }
        
        log.info("当前用户ID: {}", userId);
        return userId;
    }

    /**
     * 示例方法：检查用户权限
     */
    public boolean checkUserPermission(String operation) {
        User currentUser = UserContext.getCurrentUser();
        if (currentUser == null) {
            log.warn("用户未登录，无法执行操作: {}", operation);
            return false;
        }
        
        // 这里可以添加具体的权限检查逻辑
        log.info("用户 {} 执行操作: {}", currentUser.getEmail(), operation);
        return true;
    }

    /**
     * 示例方法：记录用户操作日志
     */
    public void logUserOperation(String operation, String details) {
        User currentUser = UserContext.getCurrentUser();
        if (currentUser != null) {
            log.info("用户操作日志 - 用户: {}, 操作: {}, 详情: {}", 
                    currentUser.getEmail(), operation, details);
        } else {
            log.warn("匿名用户操作 - 操作: {}, 详情: {}", operation, details);
        }
    }
} 